<div id="notification" class="notification error" style="display: none"></div>
<!-- Horizontal form (default) -->
<form class="horizontal-form" id="frmUser">
  <div id="formtab1">
    <fieldset class="form-part1 current">
      <legend>Email Campaign details</legend>
      <?php echo $form->renderHiddenFields() ?>
      <dl class="sections">
        <dt><?php echo $form['campaign_id']->renderLabel() ?></dt>
        <dd><?php echo $form['campaign_id']->render() ?></dd>
      </dl>
      <dl class="sections">
        <dt><?php echo $form['label']->renderLabel() ?></dt>
        <dd><?php echo $form['label']->render() ?></dd>
      </dl>
      <dl class="sections">
        <dt><?php echo $form['short_label']->renderLabel() ?></dt>
        <dd><?php echo $form['short_label']->render() ?></dd>
      </dl>
      <div class="clear" style="height:20px"></div>
    </fieldset>

  </div>


    <input id="submitUser" style="float: right;" type="button" class="small" value="Submit" />
    <input id="cancelUser" type="button" class="small" value="Back" />

    <div class="clear"></div>

</form>

<script type="text/javascript">
$(document).ready(function() {


    $("#frmUser").validationEngine();


    $("#submitUser").click(function(){

      if(jQuery("#frmUser").validationEngine('validate')) {
        toggleLoading();
        dataAr = $("#frmUser").serializeArray();
        $.ajax({
            url: '<?php echo ($form->getObject()->isNew()) ? url_for($sf_context->getModuleName().'/ajaxSave') : url_for($sf_context->getModuleName().'/update') ?>',
            type: 'POST',
            dataType: 'json',
            data: dataAr,
            success: function(response){
              $("#notification").empty();
              if(response.valid == 'false') {
                $("#notification").show();
                var errors = response.message.split('|');
                $.each(errors, function(index, value) {
                  $('#notification').append('<p>'+ this + '</p>');
                });
                var colonPos = errors[0].indexOf(':');
                var firstErrorField = errors[0].substring(0,colonPos);

              }
              else {
                $("#notification").html(response.message);
                $("#tab2-content").empty();
                $("#crud_data").click();
                jQuery("#frmUser").validationEngine('validate');
              }

              toggleLoading();
            }

        });
      }
      else {
        return false;
      }
    });

    $("#cancelUser").click(function(){
        $("#tab2-content").empty();
        $('div.formError').remove();
        $("#crud_data").click();
    });


});
</script>